package com.springboot.service;

import com.springboot.constant.CommonConstant;
import com.springboot.domain.BtDemand;
import com.springboot.domain.BtTeam;
import com.springboot.domain.BtUser;
import com.springboot.dto.EditTeamDTO;
import com.springboot.dto.TeamDemandDetailDTO;
import com.springboot.repository.DemandRepository;
import com.springboot.repository.TeamRepository;
import com.springboot.repository.UserRepository;
import com.springboot.tools.UUIDTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TeamService {

    private final TeamRepository teamRepository;
    private final DemandRepository demandRepository;
    private final UserRepository userRepository;

    @Autowired
    public TeamService(DemandRepository demandRepository,TeamRepository teamRepository,UserRepository userRepository) {
        this.demandRepository=demandRepository;
        this.teamRepository=teamRepository;
        this.userRepository=userRepository;
    }

    public boolean editTeam(EditTeamDTO editTeamDTO, String userId) {
        String demandId = editTeamDTO.getDemandId();
        BtDemand demand = demandRepository.findOne(demandId);
        //判断当前登录用户在项目中的角色
        BtTeam team = teamRepository.findByUserIdAndDemand(userId,demand);
       if (null == team || !team.getRole()){
          return false;  //无修改权限
       }
        // 删除当前项目原有成员（仅成员）
        teamRepository.deleteMembersByDemandId(demand);
        //新增项目成员
        List<String> members = editTeamDTO.getMembers();

        for ( int i = 0 ; i < members.size() - 1 ; i++ ) {
            for ( int j = members.size() -  1 ; j > i;j-- ) {
                if (members.get(j).equals(members.get(i))) {
                    members.remove(j);
                }
            }
        }
        members.forEach(uId->{
            BtTeam t = new BtTeam();
            t.setId(UUIDTool.getUuid());
            t.setCreateTime(new Date());
            t.setUserId(uId);
            t.setDemand(demand);
            t.setRole(CommonConstant.ROLE_MEMBER);
            teamRepository.save(t);
        });
        return true;
    }


    public  Page<BtDemand> findDemandByTeam(Pageable pageable, HttpSession session) {
        String id=session.getAttribute("user").toString();
        Page<BtDemand> btDemandSub = demandRepository.findDemandListByTeam(id,pageable);
        return btDemandSub;
    }

    public TeamDemandDetailDTO findDemandTeamDetail(String id) {
        TeamDemandDetailDTO teamDemandDetailDTO = demandRepository.findDemandByTeamUserId(id);
        List<BtTeam> teams=teamRepository.findByDemandId(id);
        List<BtUser> users=new ArrayList<>();
        for (BtTeam t:teams){
            BtUser user=userRepository.findByUserId(t.getUserId());
            users.add(user);
        }
        teamDemandDetailDTO.setUsers(users);
        return teamDemandDetailDTO;
    }
}
